<template>
  <div>
    <el-form
      :model="rectificationForm"
      ref="rectificationForm"
      :rules="rectificationRules"
      label-width="150px"
    >
      <el-form-item label="交办单位" prop="">
        <el-input
          v-model="rectificationForm.assign_dept_name"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="交办时间" prop="">
        <el-date-picker
          v-model="rectificationForm.assign_time"
          type="date"
          disabled
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="整改单类型" prop="">
        <el-select v-model="rectificationForm.type" value-key="" placeholder="">
          <el-option :key="1" label="整改函" :value="1"></el-option>
          <el-option :key="2" label="纪检建议" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="整改单位" prop="undertake_dept_id">
        <!-- <DeptCascader
          :defaultChecked.sync="rectificationForm.undertake_dept_id"
          :options="dept"
          @change="changeDept"
        ></DeptCascader> -->
        <!-- <el-select
          v-model="rectificationForm.undertake_dept_id"
          value-key=""
          placeholder=""
          clearable
          filterable
        >
          <el-option
            v-for="item in dept"
            :key="item.dept_id"
            :label="item.dept_name"
            :value="item.dept_id"
          >
          </el-option>
        </el-select> -->
        <el-input v-model="detail.responsible_unit_name" disabled></el-input>
      </el-form-item>
      <el-form-item label="整改期限" prop="mend_days">
        <el-input-number
          v-model="rectificationForm.mend_days"
          :step="1"
          :controls="true"
          :precision="0"
          controls-position="right"
        >
        </el-input-number>
        <span style="margin-left: 20px"> 天 </span>
      </el-form-item>
      <el-form-item label="整改建议" prop="assign_opinion">
        <el-input
          type="textarea"
          v-model="rectificationForm.assign_opinion"
          :autosize="{
            minRows: 3,
            maxRows: 6,
          }"
        ></el-input>
      </el-form-item>
      <el-form-item label="附件">
        <CommonUpload tableName="rectification" useType="send"></CommonUpload>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="sendRectification"
          >发送整改单</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCurrentDate } from "@/utils/date";
import DeptCascader from "@/components/Common/DeptCascader.vue";
import { getDeptTree } from "@/api/System/Department";
import { required } from "@/utils/eValidate";
import {
  getRectificationDept,
  postSendRectification,
} from "@/api/RedAlert/Index";
import CommonUpload from "@/components/Common/CommonUpload.vue";
import uploadMixin from "@/components/GenerateForm/uploadMixin";

export default {
  mixins: [uploadMixin],
  components: { DeptCascader, CommonUpload },
  props: {
    detail: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      rectificationForm: {
        warning_id: 0,
        warning_assign_solve_id: 0,
        // 整改单类型 1整改函 2纪检建议
        type: 1,
        // 交办单位
        assign_dept_name: "",
        // 交办时间
        assign_time: getCurrentDate(),
        // 整改单位 名称
        undertake_dept_name: "",
        // 整改单位 ID
        undertake_dept_id: "",
        // 整改期限
        mend_days: "",
        // 整改建议
        assign_opinion: "",
      },
      rectificationRules: {
        undertake_dept_id: [],
        mend_days: [required],
        assign_opinion: [required],
      },
      dept: [],
    };
  },
  methods: {
    initDept() {
      getRectificationDept().then((res) => {
        if (res.code == 0) {
          this.dept = res.data;
        }
      });
    },
    // changeDept(val) {
    //   this.rectificationForm.undertake_dept_id = val.value;
    //   this.rectificationForm.undertake_dept_name = val.label;
    // },
    sendRectification() {
      this.$refs.rectificationForm.validate((valid) => {
        if (valid) {
          this.rectificationForm.warning_id = this.detail.id;
          this.rectificationForm.warning_assign_solve_id =
            this.detail.warning_assign_solve_id;

          // this.rectificationForm.undertake_dept_name = this.dept.filter((i) => {
          //   if (i.dept_id == this.rectificationForm.undertake_dept_id) {
          //     return i;
          //   }
          // })[0].dept_name;

          this.rectificationForm.undertake_dept_id =
            this.detail.responsible_unit_id;

          this.rectificationForm.undertake_dept_name =
            this.detail.responsible_unit_name;

          postSendRectification(this.rectificationForm).then((res) => {
            if (res.code == 0) {
              this.$message.success("整改单发送成功");
              this.$emit("refresh");
              this.checkFile(res.data);
            }
          });
        }
      });
    },
  },
  created() {
    let user = JSON.parse(localStorage.getItem("user"));
    this.rectificationForm.assign_dept_name = user.dept_name;

    this.initDept();
  },
};
</script>

<style lang="scss" scoped>
</style>